package Synchronized;


public class AccountingSyncClass implements Runnable{
    //共享资源  （临界资源）
    static  int i=0;

    //锁是当前class对象
    public synchronized static void increase(){
        i++;
    }
    @Override
    public void run() {
        for(int i=0;i<1000000;i++){
            increase();
        }
    }

    public static void main1(String[] args) throws InterruptedException {
        //获得类锁--线程安全
        AccountingSyncClass accountingSyncClass1=new AccountingSyncClass();
        AccountingSyncClass accountingSyncClass2=new AccountingSyncClass();
        Thread thread1=new Thread(accountingSyncClass1);
        Thread thread2=new Thread(accountingSyncClass2);

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("i="+i);
        //输出 i=2000000


    }
    public static void main(String[] args) throws InterruptedException {
        //获得类锁 ----线程安全
        AccountingSyncClass accountingSyncClass=new AccountingSyncClass();
        Thread thread1=new Thread(accountingSyncClass);
        Thread thread2=new Thread(accountingSyncClass);

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("i="+i);
        //输出结果 i=2000000
    }
}

